// 通话备注相关逻辑 外呼、回呼列表使用
import { inComingSave } from "@/api/hrcrm/customer";
export const callNote = {
  data(){
    return {
      // 备注弹窗
      callOpen: false,
      callFormEdit : false,
      //参数
      callForm: {
        callId  : "",
        crmId   : "",
        callType: "",
        commentType: "",
        comment : "",
        save    : 0
      },
      commentId : null,
      rulesCall: {
        comment: [
          { required: true, message: "通话备注不能为空", trigger: "blur" }
        ],
      },
    }
  },
  methods: {
    // 备注
    handleComment(row, type){
      this.callFormEdit = type === "update";
      if(type === "update"){
        this.commentId = row.commentId;
        this.callForm.commentType = row.commentType;
      }
      if(row){
        this.callForm.callId = row.cdrMainUniqueId || "";
        this.callForm.crmId = row.crmId || "";
        this.callForm.callType = row.cdrCallType + "" || "";
        this.callForm.comment = row.comment || "";
        this.callOpen = true;
      }

    },
    // 保存
    submitCalled(save){
      if(this.callFormEdit){
        this.updateCallNotes()
      } else {
        this.addCallNotes(save)
      }
    },
    /** 提交按钮 */
    updateCallNotes() {
      let errStr = this.checkCallNoteForm();
      if(errStr){
        this.$modal.msgError(errStr);
        return;
      }
      let query = {
        ...this.callForm,
        id : this.commentId,
      }
      // return console.log(JSON.stringify(query, null, 2));
      this.$refs["callForm"].validate(valid => {
        if (valid) {

        }
      });
    },
    /** 话后保存 */
    addCallNotes(save) {
      let errStr = this.checkCallNoteForm();
      if(errStr){
        this.$modal.msgError(errStr);
        return;
      }
      //console.log(JSON.stringify(this.callForm, null, 2));
      this.$refs["callForm"].validate(valid => {
        if (valid) {
          this.callForm.save=save;
          inComingSave(this.callForm).then(response => {
            if (response.code == 500) {
              this.$modal.msgError(response.msg);
            } else {
              this.$modal.msgSuccess(response.msg);
              this.getList();
              this.callOpen = false;
              // 清空数据
              this.resetCallForm();
              this.$emit("action", {action: "note"})
            }
          });
        }
      });
    },
    resetCallForm(){
      // 重置表单
      Object.keys(this.callForm).forEach((key)=>{
        this.callForm[key] = "";
      });
    },
    checkCallNoteForm(){
      let errStr = ""
      if(!this.callForm.callId ){
        errStr = "缺少通话ID,通话后才能添加备注!"
      }
      if(!this.callForm.crmId ){
        errStr = "缺少crmId"
      }
      if(!this.callForm.callType ){
        errStr = "缺少通话类型"
      }
      if(!this.callForm.comment ){
        errStr = "请填写备注内容"
      }
      if(this.callFormEdit && !this.commentId){
        errStr = "缺少通话备注ID！"
      }
      return errStr
    },
  }
}
